//
//  WJYWebViewController.m
//  WWLNetease
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "WJYWebViewController.h"

@interface WJYWebViewController ()

@property (nonatomic, retain) NSMutableArray *imgArr; /**< 图片数组 */
@property (nonatomic, assign) NSInteger height; /**< 高度 */
@property (nonatomic, retain) UIAlertController *AlertC; /**< 提示框 */

@property (nonatomic, retain)UIWebView *webView;

@end

@implementation WJYWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, (667  - 64) * SIZE)];
    
    [self handleData];

    [self clearWebViewBackground:self.webView];
    
    [self.view addSubview: self.webView];

    // 添加右滑手势
//    [self addSwipeRecognizer];
    
}


#pragma mark- 边缘反弹处理
- (void)clearWebViewBackground:(UIWebView *)webView
{
    // 如果是WebView 禁止边缘反弹
    UIWebView *web = webView;
    for (id view in web.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            [view setBounces:NO];
        }
    }
}

#pragma mark- 数据处理
- (void)handleData
{
    // 拼接网址
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.docId];
//    NSLog(@"%@", url);
    
    // AFN解析
    
    [AppTools getDataWithURL:url Block:^(id result) {
        // 数组初始化
        self.imgArr = [NSMutableArray array];
        
        // 获取字典
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[result objectForKey:self.docId]];
        
        // 可变字符串
        NSMutableString *bodyStr = [NSMutableString string];
        
        // 获取内容
        bodyStr = [dic objectForKey:@"body"];
        
        
        /* 添加图片 */
        // 数组初始化
        NSMutableArray *tempArr = [NSMutableArray array];
        
        // 判断 将图片数据存入数组
        if ([[dic allKeys] containsObject:@"img"]) {
            tempArr = [dic objectForKey:@"img"];
            
            // 遍历数组
            for (NSDictionary *refDic in tempArr) {
                
                // 遍历 获取key
                for (NSString *key in refDic) {
                    
                    /* 图片自适应 */
                    // 从pixel中取出数据
                    if ([[refDic  allKeys]containsObject:@"pixel"] && ![[refDic valueForKey:@"pixel"] isEqualToString:@""]) {
                        
                        // 获取数据
                        NSString *pixelS = [NSString stringWithFormat:@"%@",[refDic objectForKey:@"pixel"]];
                        
                        // 分解字符串(x*y)
                        NSArray *pixelArr = [pixelS componentsSeparatedByString:@"*"];
                        
                        // 分别取出x,y
                        NSString *widthS = [pixelArr firstObject];
                        NSString *heightS = [pixelArr lastObject];
                        
                        // 分别转成Integer 类型
                        NSInteger width = widthS.integerValue;
                        NSInteger height = heightS.integerValue;
                        
                        // 适配高度
                        self.height = self.view.frame.size.width * height / width;
                    }
                    
                    
                    // 如果没有 高度等于450
                    if (![[refDic allKeys]containsObject:@"pixel"]) {
                        self.height = 450;
                    }
                    
                    // 通过key取出src中的数据
                    if ([key isEqualToString:@"src"]) {
                        
                        // 取出ref
                        NSRange range = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        
                        // 如果不为空
                        if (range.length != NSNotFound) {
                            
                            // 拼接
                            NSString *picStr = [NSString stringWithFormat:@"<img src=\"%@\" width=\"355\" height=\"%ld\">", [refDic objectForKey:key], self.height];
                            
                            // 拼入
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range withString:picStr].mutableCopy;
                        }
                    }
                }
            }
        }
        
        
        
        NSString *titleStr = [NSString stringWithFormat:@"<H3 ALIGN=LEFT>%@</H3>", [dic objectForKey:@"title"]];
        
        
        NSMutableArray *videoArr = [NSMutableArray array];
        /* 添加视频 */
        if ([[dic allKeys] containsObject:@"video"]) {
            videoArr = [dic objectForKey:@"video"];
            
            
            for (NSDictionary *refDic in videoArr) {
                
                
                for (NSString *key in refDic) {
                    
                    
                    if ( [key  isEqualToString:@"url_mp4"] && ![[refDic valueForKey:@"url_mp4"] isEqualToString:@""] ) {
                        
                        
                        NSRange range1 = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        
                        
                        if (range1.length != NSNotFound) {
                            
                            
                            NSString *videoStr = [NSString stringWithFormat:@"<video src=\"%@\" width=\"355\" height=\"240\" webkit-playsinline controls=\"preload\"></video>",[refDic objectForKey:key]];
                            
                            
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range1 withString:videoStr].mutableCopy;
                        }
                    }
                }
            }
        }
        
//        bodyStr = [titleStr stringByAppendingString:bodyStr].mutableCopy;
        [self.webView loadHTMLString:bodyStr baseURL:nil];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error :%@", error);
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
